package com.shujia.jinjie;

import com.shujia.utils.HbaseUtil;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

/**
 * 合理设计rowkey查找某个用户最近的10条记录
 */
public class DianXinDemo1 {
    public static void main(String[] args) throws Exception {
        //82044609CB65BA7139205E7808144D5C254C419F
        getTop10Person();

    }

    public static void getTop10Person() throws Exception{
        Table dianXinDemo1 = HbaseUtil.getOneTable("dianxin_demo1");
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入您要查询最近10条记录的用户手机号：");
        String phone = sc.next();
        //只用前缀过滤器，过滤该用户的记录
        PrefixFilter prefixFilter = new PrefixFilter(Bytes.toBytes(phone));

        //创建Scan对象
        Scan scan = new Scan();
        scan.setFilter(prefixFilter);
        scan.setLimit(10);

        //查询数据
        ResultScanner resultScanner = dianXinDemo1.getScanner(scan);
        for (Result result : resultScanner) {
            //"", "wg", "city", "qx", "stayTime", "startTime", "", "day"};
            String wg = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("wg")));
            String city = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("city")));
            String qx = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("qx")));
            String stayTime = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("stayTime")));
            String startTime = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("startTime")));
            String day = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("day")));

            //获取行键
            String rk = Bytes.toString(result.getRow());
            String phoneNum = rk.split("-")[0];
            long leaveTime = 20190000000000L - Long.parseLong(rk.split("-")[1]);

            System.out.println("手机号："+phoneNum+"网格编号："+wg
                    +"城市编号："+city
                    +"区县编号："+qx
                    +"停留时长："+stayTime
                    +"进入网格时间："+startTime
                    +"离开网格时间："+leaveTime
                    +"当天日期："+day);

        }


    }


    public static void putDianXinData() throws Exception{
        HbaseUtil.createOneTable("dianxin_demo1", "info");

        BufferedReader br = new BufferedReader(new FileReader("hbase/data/dianxin_data"));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (!line.contains("shushan") && !line.contains("\\N")) {
                String[] infos = line.split("\t");
                //将手机号与离开网格的时间戳拼接，较大的时间戳值减去离开网格的时间戳再进行拼接
                //20180503172154 -- （20190000000000-20180503172154）=> 最大
                //20180503180350 --
                //20180503233517 -- （20190000000000-20180503172154）=> 最小
                String rowkey = infos[0] + "-" + (20190000000000L - Long.parseLong(infos[6]));
                String[] colNameArray = {"", "wg", "city", "qx", "stayTime", "startTime", "", "day"};

                for (int i = 1; i < infos.length; i++) {
                    if (i == 6) {
                        continue;
                    }
                    //封装成一个KeyValue的对象
                    //  public KeyValue(final byte [] row, final byte [] family, final byte [] qualifier, final byte [] value)
                    KeyValue keyValue = new KeyValue(Bytes.toBytes(rowkey),
                            Bytes.toBytes("info"),
                            Bytes.toBytes(colNameArray[i]),
                            Bytes.toBytes(infos[i]));

                    HbaseUtil.putOneColData("dianxin_demo1", rowkey, "info", colNameArray[i], infos[i]);
                }

            }
        }
    }
}
